<template>
    <div id="systemDictEdit__">
        <table class="layui-hide" id="systemDictEdit_" lay-filter="systemDictEdit_"></table>
        <script type="text/html" id="toolbar">
          <div class="layui-btn-container">
            <button class="el-button el-button--success el-button--mini is-round"  lay-event="add">新增</button>
            <button class="el-button el-button--success is-round el-button--mini" lay-event="refresh">刷新列表</button>
          </div>
        </script>
        <script type="text/html" id="bar">
          <button class="el-button el-button--primary el-button--mini is-round" lay-event="edit" >编辑</button>
          <button class="el-button el-button--danger el-button--mini is-round" lay-event="del" >删除</button>
        </script>
    </div>
</template>
<script>
    export default {
        name: "index",
        props:['data','search_'],
        watch:{
            search_:function () {
                this.search();
            }
        },
        data() {
            var that = this;
            return {
                table_head:[
                    {field: 'id',  title: '编号', sort: true}
                    , {field: 'code_type_name',  title: '类别', sort: true}
                    , {field: 'display_value',  title: '内容', sort: true}
                    , {field: 'memo',  title: '拼音简码/备注', sort: true}
                    , {fixed: 'right',width: 165,align:'center',templet: '#bar'}
                ],
                datas:{
                    code_type:that.data.code_type,
                    code_type_name:decodeURI(that.data.code_type_name),
                    display_value:"",
                    flag:1,
                    code_id:0,
                    memo:"",
                },
                disabled:{
                    code_type_name:true,
                },
                rules: {
                    code_type_name: [
                        {required: true, message: '请输入类别', trigger: 'blur'},
                    ],
                    display_value: [
                        {required: true, message: '请输入名称', trigger: 'blur'},
                    ],
                },
                type:"",// edit/add
            }
        },
        methods:{
            search:function(){
                var config = {
                    elem: '#systemDictEdit_'
                    , url: burl + 'basic-information-system-dict/list/' //数据接口
                    , method:'post'
                    , where:{CrmSystemDictSearch: {code_type: this.data.code_type}}
                    , toolbar: '#toolbar'
                    , title: '系统字典'
                    , height: 580
                    , totalRow: true
                    , cols: [this.table_head]
                    , page: true //开启分页
                };
                // 再次加载出表格数据（之前已经加载出来了dom数据）
                layui.use('table', () => {//加这个是防止刷新的时候会出现不存在render方法
                    layui.table.render(config);
                    //头工具栏事件
                    layui.table.on('toolbar(systemDictEdit_)', (obj) => {
                        switch(obj.event){
                            case 'add':
                                this.type = "add"
                                this.alertAdd();
                                this.$parent.datas.code_id = obj.config.page.count+1;
                                break;
                            case 'refresh':
                                layui.table.render(config);
                                break;
                        };
                    });
                    //监听行工具事件
                    layui.table.on('tool(systemDictEdit_)', (obj) => {
                        switch(obj.event){
                            case 'edit':
                                this.type = "edit"
                                this.alertAdd(obj);
                                break;
                            case 'del':
                                allIndex = layer.confirm("确认是否删除该文件！",() => {
                                    this.$post('basic-information-system-dict/delete?id='+obj.data.id,{})
                                        .then((response) => {
                                            if (response.data.res == 1) {//成功
                                                this.$message({message:"删除成功！",type:"success"})
                                                obj.del()
                                                layer.close(allIndex)
                                            }else{//失败
                                                this.$message({message:"删除失败！",type:"error"})
                                            }
                                        }).catch((response) => {
                                        this.$message({message:"错误！",type:"error"})
                                    });
                                });
                                break;
                        };
                    });
                });
            },
            alertAdd: function (obj) {
                if (obj!=null){
                    this.$parent.datas = obj.data;
                }else{
                    this.$parent.datas = {
                        code_type:this.data.code_type,
                        code_type_name:decodeURI(this.data.code_type_name),
                        display_value:"",
                        flag:1,
                        code_id:0,
                        memo:"",
                    };
                }
                layui.use('layer',()=>{
                    //编辑页面页面层
                    allIndex = layer.open({
                        type: 1,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['40%', '70%'], //宽高
                        close: true,
                        shadeClose: true,
                        content:$("#addEdit_"),
                        end: () => {//关闭隐藏
                            if (this.type=="edit") {
                                if (allSuccess=='success'){
                                    obj.update(this.$parent.datas)
                                    allSuccess = 'faild'
                                }
                            }
                        }
                    });
                })
            },
        }
    }
</script>